package org.easy.excel.support.config.impl;

import com.alibaba.excel.write.handler.WriteHandler;
import org.easy.excel.support.config.AnnotationExcelConfig;
import org.easy.excel.support.handler.AnnotationColumnWidthWriteHandler;
import org.easy.excel.support.handler.AnnotationHeadRowWriteHandler;
import org.easy.excel.support.parse.AnnotationExcelSourceParse;
import org.easy.excel.support.parse.source.ExportLabelSource;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 简单的 {@link AnnotationExcelConfig} 实现类
 *
 * @author 李坤
 * @date 2022/6/9 11:03
 */
public class AsyncAnnotationExcelConfig extends AbstractAnnotationExcelConfig {

    /**
     * {@link AsyncAnnotationExcelConfig} 的唯一构造函数
     *
     * @param annotationExcelSourceParse 注解-数据源解析器
     * @param sourceClass                解析的数据类型
     */
    public AsyncAnnotationExcelConfig(AnnotationExcelSourceParse annotationExcelSourceParse, Class<?> sourceClass, Integer dataLineSize) throws Exception {
        final List<ExportLabelSource> annotationExcelSourceList = annotationExcelSourceParse.getAnnotationExcelSourceList(sourceClass);
        this.writeHandlerList = Arrays.asList(new AnnotationHeadRowWriteHandler(dataLineSize, this),
                new AnnotationColumnWidthWriteHandler(annotationExcelSourceList));
    }

    /**
     * {@link AsyncAnnotationExcelConfig} 的唯一构造函数
     *
     * @param annotationExcelSourceParse 注解-数据源解析器
     * @param writeHandlerList           样式设置拦截器
     * @param sourceClass                解析的数据类型
     */
    public AsyncAnnotationExcelConfig(AnnotationExcelSourceParse annotationExcelSourceParse,
                                      List<WriteHandler> writeHandlerList, Class<?> sourceClass) throws Exception {
        this.writeHandlerList = writeHandlerList;
        this.annotationExcelSourceParse = annotationExcelSourceParse;
        final List<ExportLabelSource> annotationExcelSourceList = annotationExcelSourceParse.getAnnotationExcelSourceList(sourceClass);
    }

    @Override
    protected List<List<String>> getExcelMiddleHeaderList(int length) {
        final List<List<String>> outerCollection = new ArrayList<>(length);

        for (int index = 0; index < length; index++) {
            if (index == 0 || index == 1) {
                outerCollection.add(index, Collections.singletonList("日期：" + LocalDate.now()));
            } else if (index == length - 1 || index == length - 2) {
                outerCollection.add(index, Collections.singletonList("单位：元 / %"));
            } else {
                outerCollection.add(index, Collections.singletonList(""));
            }
        }
        return outerCollection;
    }

}
